﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace CloudShot.Core.Utils
{
  public static class StringHelpers
  {
    #region String Encryption
    //TODO: maybe will be better create EncryptedString class (non static) and move all string encryption operations there?

    private static readonly byte[] _salt = Encoding.Unicode.GetBytes("wp6otxbX7AR" + SystemInformation.ComputerName + "ScTAaz8q4UV");

    public static string EncryptString(this string input, DataProtectionScope protectionScope = DataProtectionScope.CurrentUser)
    {
      byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(input), _salt, protectionScope);
      return Convert.ToBase64String(encryptedData);
    }

    public static string DecryptString(this string encryptedData, DataProtectionScope protectionScope = DataProtectionScope.CurrentUser)
    {
      try
      {
        byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(encryptedData), _salt, protectionScope);
        return Encoding.Unicode.GetString(decryptedData);
      }
      catch
      {
        return string.Empty;
      }
    }

    #endregion

    public static bool Contains(this string input, string value, StringComparison comparisonType)
    {
      //just same as string.Contains implementation but with comparisonType
      return input.IndexOf(value, comparisonType) >= 0;
    }
  }
}